<template>
  <CompOperate
    :field="field"
    :designer="designer"
    :design-state="designState"
    :parent-list="parentList"
    :parent-widget="parentWidget"
    :sub-form-row-id="subFormRowId"
    :sub-form-row-index="subFormRowIndex"
    :sub-form-col-index="subFormColIndex"
    :index-of-parent-list="indexOfParentList"
    :display-style="field.options.displayStyle"
  >
    <div class="scene-report-info-widget" :style="getStyle('head')">
      <template v-if="true">
        <div v-if="showComp(0)" class="report-home">
          <ReportKeyboard :field="field" @handleSubmit="handleSubmit" />
        </div>
        <div v-if="showComp(1)" class="report-result">
          <ReportResult
            :state="1"
            :field="field"
            :devType="devType"
            @handleReport="handleReport"
            @handleReportAll="handleReportAll"
            @handleSignPrint="handleSignPrint"
          />
          <!--底部操作按钮-->
          <div class="operate-info" v-if="true">
            <div class="back-home" @click="handleBack">
              <Icon name="wap-home" />
              <span class="back-text"
                >返回首页({{ backcountdown / 1000 }}s)</span
              >
            </div>
          </div>
        </div>
      </template>
    </div>
  </CompOperate>
</template>

<script>
import { Field, Icon } from "vant";
import emitter from "@/utils/designer/emitter";
import { pxToVw } from "@/utils/common";
import { buildParentApi } from "@/utils/designer/util";
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import ReportResult from "./reportComps/reportResult.vue";
import ReportKeyboard from "./reportComps/reportKeyboard.vue";
import SimpleKeyboard from "@c/public/layer/scenes/simpleKeyboard/index.vue";
import * as types from "@c/public/form-designer/devType/datas-types";

export default {
  name: "scene-report-info-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    SimpleKeyboard,
    Field,
    Icon,
    CompOperate,
    ReportResult,
    ReportKeyboard,
  },

  props: {
    widget: Object,
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },
    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
    //prop传入的表单JSON配置
    parentApi: {
      type: Object,
      default: () => buildParentApi(),
    },
    devType: {
      type: Boolean,
      default: false,
    },
  },

  data() {
    return {
      number: 0,
      step: this.field.options.reportInfoConfig.step,
      backcountdown: 120000,
      optionsInfo: Object(),
    };
  },

  computed: {
    // 设置的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = self.field?.options?.reportInfoConfig || Object();
        let Obj = Object();
        switch (val) {
          case "head":
            Reflect.set(Obj, "height", pxToVw(info.headHeight));
            Reflect.set(Obj, "backgroundColor", `${info.backgroundColor}`);
            return Obj;
          case "picture":
            Reflect.set(Obj, "height", pxToVw(info.imageHeight));
            Reflect.set(Obj, "width", pxToVw(info.imageWidth));
            Reflect.set(Obj, "borderRadius", pxToVw(info.imageRadius));
            return Obj;
        }
      };
    },

    //步距展示对应的组件
    showComp() {
      return function (val) {
        return this.optionsInfo.step === val;
      };
    },

    // 获取父级传递过来的step
    getBackcountdown() {
      this.$nextTick(() => {
        let { backcountdown } = this.field.options.reportInfoConfig;
        return backcountdown * 1000 || this.backcountdown;
      });
    },
  },

  created() {
    this.registerToRefList();
    this.initEventHandler();
    this.handleOnCreated();
  },

  mounted() {
    this.handleOnMounted();
    console.log("info");
  },

  methods: {
    handleClick() {
      this.number = ++this.number;
    },
    // 提交号别
    handleSubmit(params) {
      this.parentApi.executeParentMethod("handleReport", params);
    },

    // 单独报到
    handleReport(params) {
      console.log("单独报到");
      this.parentApi.executeParentMethod("toReport", params);
    },

    // 批量报到
    handleReportAll(params) {
      console.log("批量报到");
      this.parentApi.executeParentMethod("reportAll", params);
    },

    handleSignPrint(params) {
      console.log("info-打印小票");
      this.parentApi.executeParentMethod("signPrint", params);
    },

    // 接收父组件传递过来的数据
    executeMethod(key, resp) {
      switch (key) {
        case "reportPatientInfo":
          let { code, data } = resp;
          if (code === 200) {
            this.optionsInfo.step = 1;
            this.$store.dispatch(
              "setReportPatientInfo",
              this.$lodash.cloneDeep(data)
            );
          }
          break;
        case "reportBack":
          console.log("executeMethod");
          Reflect.set(this.optionsInfo, "step", 0);
          break;
      }
    },

    handleBack2() {
      this.executeMethod("reportBack");
    },

    // 返回首页
    handleBack() {
      this.optionsInfo.step = 0;
      this.parentApi.executeParentMethod("openReadCardScan");
    },
  },

  watch: {
    // 步距
    "optionsInfo.step": {
      handler: function (val) {
        const self = this;
        this.optionsInfo.step = parseInt(val);
        // 步骤过程发生变化--执行时间的倒计时
        if (parseInt(val) === 1) {
          this.parentApi.executeParentMethod("openReadCardScan");
          self.timer = setInterval(() => {
            self.backcountdown = self.backcountdown - 1000;
            // 到达时间--返回首页-清空定时器
            if (self.backcountdown <= 0) {
              self.handleBack();
              clearInterval(self.timer);
            }
          }, 1000);
        } else {
          clearInterval(self.timer);
        }
      },
      deep: true,
      immediate: true,
    },

    // 监听父级传递过来的props--field
    field: {
      handler: function (val) {
        let key = val.options["config"];
        if (Reflect.has(val.options[key], "patientShowFields")) {
          Reflect.set(
            val.options[key],
            "patientShowFields",
            types["patientFields"]
          );
        }
        this.optionsInfo = val.options[key];
        this.backcountdown = this.optionsInfo.backCountdown * 1000 || 120000;
        this.$store.dispatch("setReportStepInfo", this.optionsInfo.step);
      },
      deep: true,
      immediate: true,
    },
  },

  destroyed() {
    this.unregisterFromRefList();
    this.$store.dispatch("setReportStepInfo", 0);
  },
};
</script>

<style lang="scss" scoped>
.scene-report-info-widget {
  font-size: 60px;
  overflow: hidden;
  background: #dcdee2 !important;
  margin-top: -40px;
  border-radius: 28px 28px 0 0;
  box-sizing: border-box;
  //报到首页
  .report-home {
    height: calc(100% - 28px);
    //overflow: hidden;
    box-sizing: border-box;
    background: transparent !important;
    margin: 0 30px 0 30px;
    padding-top: 14px;
  }

  //报到结果
  .report-result {
    height: 100%;
    width: 100%;
    overflow: hidden;

    &::-webkit-scrollbar {
      width: 0;
      height: 0;
      position: relative;
      z-index: 99999999999;
    }

    .operate-info {
      position: absolute;
      left: 50%;
      bottom: 0;
      transform: translate(-50%, 0);

      .back-home {
        align-self: center;
        background: rgba(0, 0, 0, 0.8);
        color: white;
        padding: 24px 52px;
        font-size: 44px;
        border-radius: 24px 24px 0 0;

        .van-icon {
          font-size: 58px;
          padding-right: 20px;
        }
      }
    }

    .operate-info2 {
      width: 100%;
      bottom: 0;
      display: flex;
      justify-content: space-between;
      position: absolute;
      background: #ffffff;
      box-shadow: 0 2px 20px 0 rgba(0, 0, 0, 0.1);

      .left {
        display: flex;
        align-items: center;
        align-self: center;
        padding-left: 42px;
        color: #333333;
        font-size: 46px;
      }

      .right {
        display: flex;
        background: rgba(0, 89, 179, 1);
        color: white;
        align-items: center;
        align-self: center;
        border-radius: 100px 0 0 100px;
        padding: 14px 18px;

        .countdown {
          width: 94px;
          height: 94px;
          line-height: 94px;
          border-radius: 60px;
          font-size: 32px;
          text-align: center;
          flex-shrink: 0;
          background: rgba(255, 255, 255, 0.2);
          margin-right: 38px;
        }

        .operate-area {
          font-size: 38px;
          display: flex;

          .item {
            display: flex;
            flex-direction: column;
            padding: 0 36px;

            .operate-icon {
              text-align: center;
              font-size: 50px;
            }

            .operate-desc {
              margin-top: 14px;
            }
          }
        }
      }
    }
  }
}
</style>